package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

/**
 * Servlet implementation class GoodsDetailServlet
 */
@WebServlet("/index/detail")
public class GoodsDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	private IGoodsService goodsService = new GoodsServiceImpl();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取 [宠物分类] 列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request中, 供/index/detail.jsp使用
		request.setAttribute("typeList", typeList);
		
		//获取前端传递过来的goodid
		String goodsIdStr = request.getParameter("goodid");
		//把字符串转换为整型
		int goodsId = Integer.parseInt(goodsIdStr);
		
		//根据宠物的id查询宠物
		Goods goods = goodsService.getGoodsById(goodsId);
		//把goods存入request中, 供/index/detail.jsp使用
		request.setAttribute("good", goods);
		
		//请求转发,跳转到/index/detail.jsp
		request.getRequestDispatcher("/index/detail.jsp").forward(request, response);
	}

}
